// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class LoadPage extends StatefulWidget {
  const LoadPage({super.key});
  @override
  State<LoadPage> createState() => _LoadPageState();
}

class _LoadPageState extends State<LoadPage> {
  @override
  void initState() {
    super.initState();
    setupData();

  }

  setupData() async {
    Network.post(url_getAppUpgradeInfo).then((res) async {

      if (!res.success) {
        toast(res.msg);
        await Future.delayed(const Duration(seconds: 2));
        setupData();
        return;
      }

      if (res.ret is Map) {
        getTime();
      }

      await Future.delayed(const Duration(seconds: 0));

      if (res.data is! Map) {
        jump2Tab(res.data["build_number"],1);
        return;
      }

      checkVersion(res.data);
    });
  }

  getTime() {
    Network.post(url_getCurtime).then((respon) {
      if (!respon.success) {
        getTime();
        return;
      }

      updateCurrentTime(respon.ret["timestamp"]);
    });
  }

  checkVersion(data) async {
    final packageInfo = await PackageInfo.fromPlatform();
  //  setReviewState(data["build_number"] == packageInfo.buildNumber);
   
    jump2Tab(data["build_number"],packageInfo.buildNumber);
  }



  jump2Tab(number,packageInfo) async {

    Timer(Duration(milliseconds:1), () {
     Navigator.of(context).pushReplacementNamed(PageMainTab, arguments:{"number":number,"packageInfo":packageInfo});
    });

  }


  @override
  Widget build(BuildContext context) {
 final totalHeight = MediaQuery.of(context).size.width;
   return Scaffold(
      backgroundColor: Color(0XFF0c1012),
      body: Align(
        alignment: Alignment.center,
        child: QGImage(asset: "hlogins.png", fit: BoxFit.fill,width: totalHeight),
      )
      
     
    );
    // if (!kIsWeb) {
    //    if (Platform.isIOS) {
    //     return QGImage(asset: "load_image.jpg");
    //   }
    // }
    // return QGImage(asset: "load_image.jpg", fit: BoxFit.fill);
  }
}
